package com.kumi.shop.ware.service.impl;

import com.kumi.common.constant.WareConstant;
import com.kumi.shop.ware.bean.PurchaseDetail;
import com.kumi.shop.ware.service.PurchaseDetailService;
import com.kumi.shop.ware.service.WareSkuService;
import com.kumi.shop.ware.vo.ItemVo;
import com.kumi.shop.ware.vo.MergeVo;
import com.kumi.shop.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kumi.common.utils.PageUtils;
import com.kumi.common.utils.Query;

import com.kumi.shop.ware.mapper.PurchaseMapper;
import com.kumi.shop.ware.bean.Purchase;
import com.kumi.shop.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    private WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Purchase> page = this.page(
                new Query<Purchase>().getPage(params),
                new QueryWrapper<Purchase>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        IPage<Purchase> page = this.page(
                new Query<Purchase>().getPage(params),
                new QueryWrapper<Purchase>().eq("status",1).or().eq("status",0)
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void mergePurchase(MergeVo mergeVo) {
        Long id = mergeVo.getPurchaseId();
        if (id == null){
            Purchase purchase = new Purchase();
            purchase.setStatus(WareConstant.PurchaseEnum.CREATED.getCode());
            purchase.setCreateTime(new Date());
            purchase.setUpdateTime(new Date());
            this.save(purchase);
            id = purchase.getId();
        }

        // TODO 确认采购单状态是0,，1才可以合并
        List<Long> items = mergeVo.getItems();
        Long finalId = id;
        List<PurchaseDetail> collect = items.stream().map(i -> {
            PurchaseDetail detail = new PurchaseDetail();
            detail.setId(i);
            detail.setPurchaseId(finalId);
            detail.setStatus(WareConstant.PurchaseDetailEnum.ASSIGNED.getCode());
            return detail;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
        Purchase purchase = new Purchase();
        purchase.setId(id);
        purchase.setUpdateTime(new Date());
        this.updateById(purchase);
    }

    @Override
    public void received(List<Long> ids) {
        List<Purchase> collect = ids.stream().map(id -> {
            Purchase byId = this.getById(id);
            return byId;
        }).filter(item -> {
            if (item.getStatus() == WareConstant.PurchaseEnum.CREATED.getCode() ||
                    item.getStatus() == WareConstant.PurchaseEnum.ASSIGNED.getCode()) {
                return true;
            }
            return false;
        }).map(item -> {
            item.setStatus(WareConstant.PurchaseEnum.RECEIVE.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(collect);

        collect.forEach((item) -> {
            List<PurchaseDetail> list = purchaseDetailService.listDetailByPurchaseId(item.getId());
            list.stream().map(entity -> {
                PurchaseDetail purchaseDetail = new PurchaseDetail();
                purchaseDetail.setId(entity.getId());
                purchaseDetail.setStatus(WareConstant.PurchaseDetailEnum.BUYING.getCode());
                return purchaseDetail;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(list);
        });
    }

    @Transactional
    @Override
    public void doen(PurchaseDoneVo doneVo) {
        Long id = doneVo.getId();
        Boolean flag = true;
        List<ItemVo> items = doneVo.getItems();

        List<PurchaseDetail> updates = new ArrayList<>();
        for (ItemVo item : items) {
            PurchaseDetail purchaseDetail = new PurchaseDetail();
            if (item.getStatus() == WareConstant.PurchaseDetailEnum.HASERRO.getCode()){
                flag = false;
                purchaseDetail.setStatus(item.getStatus() );
            }else{
                purchaseDetail.setStatus(WareConstant.PurchaseDetailEnum.FINISH.getCode());
                PurchaseDetail byId = purchaseDetailService.getById(item.getItemId());
                wareSkuService.addStock(byId.getSkuId(),byId.getWareId(),byId.getSkuNum());
            }
            purchaseDetail.setId(item.getItemId());
            updates.add(purchaseDetail);
        }
        purchaseDetailService.updateBatchById(updates);

        Purchase purchase = new Purchase();
        purchase.setId(id);
        purchase.setStatus(flag?WareConstant.PurchaseEnum.FINISH.getCode() : WareConstant.PurchaseEnum.HASERRO.getCode());
        purchase.setUpdateTime(new Date());
        this.updateById(purchase);


    }

}